﻿using DevExpress.XtraReports.UI;
using DevExpress.XtraReports.Web.WebDocumentViewer;
using ReportDesignerServer.Common;
using ReportDesignerServer.Services;

internal class CustomWebDocumentViewerReportResolver : IWebDocumentViewerReportResolver
{
    readonly string ReportDirectory;
    readonly DataSourceService service;
    const string FileExtension = ".repx";
    public CustomWebDocumentViewerReportResolver(IWebHostEnvironment env, DataSourceService service)
    {
        ReportDirectory = Path.Combine(Environment.CurrentDirectory, "Reports");
        NLogHelper.GetLog().Info("路径1:" + Environment.CurrentDirectory);
        NLogHelper.GetLog().Info("路径2:" + env.ContentRootPath);
        if (!Directory.Exists(ReportDirectory))
        {
            Directory.CreateDirectory(ReportDirectory);
        }

        this.service = service;
    }

    public XtraReport Resolve(string reportEntry)
    {
      
        try
        {
            var paras = reportEntry.Split('|');
            var fileName = paras[0];
            var args = paras.Skip(1).ToArray();
            var filePath = Path.Combine(ReportDirectory, fileName + FileExtension);
            var report = XtraReport.FromFile(filePath);
            report.DataSource = service.GetJsonDataSource(fileName, args);
            return report;
        }
        catch (Exception ex)
        {
            NLogHelper.GetLog().Error("Resolve报错："+ex.ToString());
            return new XtraReport();
        }
       
    }

}